package com.hengpeng.itfints.service.activity.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.util.MoneyUtil;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.itfinbase.en.RewardRebateStatus;
import com.hengpeng.itfinbase.en.RewardRedPacketStatus;
import com.hengpeng.itfinbase.en.RewardType;
import com.hengpeng.itfinbase.en.RewardUseType;
import com.hengpeng.itfinbase.persist.Reward;
import com.hengpeng.itfinbase.persist.RewardRebate;
import com.hengpeng.itfinbase.persist.RewardRedpacket;
import com.hengpeng.itfints.controller.activity.vo.RewardRedpacketVo;
import com.hengpeng.itfints.service.BaseService;
import com.hengpeng.itfints.service.activity.ActivityService;
import com.hengpeng.itfints.tools.DateTimeUtil;

/**
 * 活动service(返利，红包)
 * @author lugaofei
 *
 */
@Service
public class ActivityServiceImpl extends BaseService implements ActivityService {

    @Override
    public ServiceResult<List<RewardRebate>> findRewardRebateByStatus(Long userId,RewardRebateStatus status, int curPage, int pageSize,String startTime,String endTime) {
        DynamicQuery query = new DynamicQuery(RewardRebate.class);
        if (status!=null) {
            query.eq("status", status);
        }else {
            query.eq("status", RewardRebateStatus.CANPAY);
        }
        if (userId!=null) {
            query.eq("userId", userId);
        }
        if (!StringHelper.isEmpty(startTime)) {
            query.ge("date_format(gettime,'%Y-%m-%d')", startTime);
        }
        if (!StringHelper.isEmpty(endTime)) {
            query.le("date_format(gettime,'%Y-%m-%d')", endTime);
        }
        query.setPageSize(pageSize);
        query.setPageIndex(curPage);
        query.desc("gettime");
        query.setIsSearchTotalCount(Boolean.TRUE);
        QueryResult<RewardRebate> result = serviceFactory.getPersistService().findWithPage(RewardRebate.class, query);
        Long count = result.getTotalCount();
        ServiceResult<List<RewardRebate>> serviceResult = new ServiceResult<List<RewardRebate>>();// 设置返回值
        serviceResult.setReturnValue(result.getResults());
        serviceResult.setReturnListSize(count.intValue());
        return serviceResult;
    }

    @Override
    public ServiceResult<List<RewardRedpacketVo>> findRewardRedpacket(Long userId, RewardRedPacketStatus status,
            int curPage, int pageSize) {
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class,"a");
        query.createAlias(Reward.class, "b");
        query.eqProperty("a.rewardId", "b.id");
        query.addProjection(Projections.property("a.money"));//0
        query.addProjection(Projections.property("b.rewardUseType"));//1
        query.addProjection(Projections.property("b.deadLine"));//2
        query.addProjection(Projections.property("b.rewardType"));//3
        query.addProjection(Projections.property("a.status"));//4
        query.addProjection(Projections.property("a.gettime"));//5
        query.addProjection(Projections.property("a.endtime"));//6
        query.addProjection(Projections.property("a.id"));//7
        //query.inArray(, params)
        if (userId!=null) {
            query.eq("a.userId", userId);
        }
        if (status!=null) {
            query.eq("a.status", status);
        }
        query.setPageSize(pageSize);
        query.setPageIndex(curPage);
        query.setQueryType(QueryType.JDBC);
        query.setIsSearchTotalCount(Boolean.TRUE);
        query.desc("a.gettime");
        QueryResult result = serviceFactory.getPersistService().findWithPage(null, query);
        ServiceResult<List<RewardRedpacketVo>> serviceResult = new ServiceResult<List<RewardRedpacketVo>>();
        if (result != null && result.getResults().size() > 0) {
            Long count = result.getTotalCount();
            serviceResult.setReturnListSize(count.intValue());
            List l = result.getResults();
            List<RewardRedpacketVo> lists = new ArrayList<RewardRedpacketVo>();
            for (int i = 0; i < l.size(); i++) {
                Object[] obj = (Object[]) l.get(i);
                RewardRedpacketVo vo = new RewardRedpacketVo();
                vo.setMoney(Long.parseLong(String.valueOf(obj[0])));
                vo.setRewardUseType(RewardUseType.valueOf(String.valueOf(obj[1])));
                vo.setDeadLine(Integer.valueOf(String.valueOf(obj[2])));
                vo.setRewardType(RewardType.valueOf(String.valueOf(obj[3])));
                vo.setStatus(RewardRedPacketStatus.valueOf(String.valueOf(obj[4])));
                Date gettime = DateTimeUtil.formatToDate(String.valueOf(obj[5]), "yyyy-MM-dd HH:mm:ss");
                Date endtime = DateTimeUtil.formatToDate(String.valueOf(obj[6]), "yyyy-MM-dd HH:mm:ss");
                vo.setGettime(gettime);
                vo.setEndtime(endtime);
                vo.setId(Long.valueOf(String.valueOf(obj[7])));
                lists.add(vo);
            }
            serviceResult.setReturnValue(lists);
        }
        return serviceResult;
    }

    @Override
    public ServiceResult<List<RewardRedpacketVo>> findRegRedpacketByStatus(Long userId, int type,RewardRedPacketStatus rewardRedPacketStatus,
            int curPage, int pageSize) {
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class,"a");
        query.createAlias(Reward.class, "b");
        query.eqProperty("a.rewardId", "b.id");
        query.addProjection(Projections.property("a.money"));//0
        query.addProjection(Projections.property("b.rewardUseType"));//1
        query.addProjection(Projections.property("b.deadLine"));//2
        query.addProjection(Projections.property("b.rewardType"));//3
        query.addProjection(Projections.property("a.status"));//4
        query.addProjection(Projections.property("a.gettime"));//5
        query.addProjection(Projections.property("a.endtime"));//6
        
        //query.inArray(, params)
        if (userId!=null) {
            query.eq("a.userId", userId);
        }
        
        if (type==1) {//注册红包
            String[] params = {"REGISTER_RED","INVITE_ONE_RED","INVITE_TWO_RED","INVITE_THREE_RED"};
            query.inArray("b.rewardType", params);
        }else if (type==2) {
            query.eq("b.rewardType", "INVEST_RED");
        }else if (type==3) {
            String[] params = {"SHARE_ONE_RED","SHARE_TWO_RED","SHARE_THREE_RED","SHARE_FOUR_RED","SHARE_FIVE_RED","SHARE_SIX_RED"};
            query.inArray("b.rewardType", params);
        }
        query.eq("a.status", rewardRedPacketStatus);
        query.setPageSize(pageSize);
        query.setPageIndex(curPage);
        query.setQueryType(QueryType.JDBC);
        query.setIsSearchTotalCount(Boolean.TRUE);
        query.desc("a.gettime");
        QueryResult result = serviceFactory.getPersistService().findWithPage(null, query);
        ServiceResult<List<RewardRedpacketVo>> serviceResult = new ServiceResult<List<RewardRedpacketVo>>();
        if (result != null && result.getResults().size() > 0) {
            Long count = result.getTotalCount();
            serviceResult.setReturnListSize(count.intValue());
            List l = result.getResults();
            List<RewardRedpacketVo> lists = new ArrayList<RewardRedpacketVo>();
            for (int i = 0; i < l.size(); i++) {
                Object[] obj = (Object[]) l.get(i);
                RewardRedpacketVo vo = new RewardRedpacketVo();
                vo.setMoney(Long.parseLong(String.valueOf(obj[0])));
                vo.setRewardUseType(RewardUseType.valueOf(String.valueOf(obj[1])));
                vo.setDeadLine(Integer.valueOf(String.valueOf(obj[2])));
                vo.setRewardType(RewardType.valueOf(String.valueOf(obj[3])));
                vo.setStatus(RewardRedPacketStatus.valueOf(String.valueOf(obj[4])));
                Date gettime = DateTimeUtil.formatToDate(String.valueOf(obj[5]), "yyyy-MM-dd HH:mm:ss");
                Date endtime = DateTimeUtil.formatToDate(String.valueOf(obj[6]), "yyyy-MM-dd HH:mm:ss");
                vo.setGettime(gettime);
                vo.setEndtime(endtime);
                lists.add(vo);
            }
            serviceResult.setReturnValue(lists);
        }
        return serviceResult;
    }
    
    @Override
    public String getRegRedpacketTotalAmountByUserNameAndStatus(long userId,RewardRedPacketStatus[] stastus){
        DynamicQuery query = new DynamicQuery(RewardRedpacket.class);
        query.addProjection(Projections.sum("money", "moneySum"));
        query.setQueryType(QueryType.JDBC);
        if (null != stastus) {
            query.inArray("status", stastus);
        }
        query.eq("userid", userId);
        List list = serviceFactory.getPersistService().find(null, query);
        if (null == list || list.isEmpty()) {
            return "0";
        } else {
            return MoneyUtil.convertFenToYuan(Long.parseLong(list.get(0) == null ? "0" : list.get(0) + ""));
        }
    }

}
